 /**
  * 
  * 
  * 根据页面统一管理按钮状态
  * 
  */


let buttons = null;

let buttonVisible = {};
let buttonDisable = {};


export default function(page) {

    const {
        allButtons
    } = this.state;

    const {button} = this.props;

    if(!buttons) {
        let childs = allButtons.concat();
        buttons = [];
        while(childs.length > 0) {
            let current = childs.shift();
            buttons.push(current.key);
            if(current.children && current.children.length > 0) {
                childs = childs.concat(current.children);
            }
        };
    }

    buttons.forEach((item) => {
        buttonVisible[item] = false;
        buttonDisable[item] = true;
    });

    let changeDisabled = [];
    let changeVisible = [];

    // 初始化
    if(page === 'init') {
        changeVisible = ['Query', 'Refresh', 'moreop', 'BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'BatchAddSub', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'refadd', '-Edit', '-Delete', 'stop', 'cardrpt'];
        changeDisabled = ['moreop'];
    }
    // 获取主组织之后释放的按钮
    else if(page === 'hasMainOrg') {
        changeVisible = ['Query', 'Refresh', 'moreop', 'BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'BatchAddSub', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'refadd', '-Edit', '-Delete', 'stop', 'grpadd', 'grpedit', 'grpdel', 'grpref', 'grpena', 'grpdis', 'cardrpt'];
        changeDisabled = ['Query', 'Refresh', 'moreop', '-refadd', 'grpadd', 'grpedit', 'grpdel', 'grpref', 'grpena', 'grpdis'];
    }
    // 主表有数据
    else if(page === 'mainTableHasData') {
        changeVisible = ['Query', 'Refresh', 'moreop', 'BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'BatchAddSub', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'refadd', '-Edit', '-Delete', 'stop', 'grpadd', 'grpedit', 'grpdel', 'grpref', 'grpena', 'grpdis', 'cardrpt'];

        changeDisabled = ['Query', 'Refresh', 'moreop', '-BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'BatchAddSub', '-refadd', '-Edit', 'grpedit', 'grpref', 'grpena', 'grpdis'];
    }
    // 主表有被选中的行
    else if(page === 'hasSelectedRow') {
        changeVisible = ['Query', 'Refresh', 'moreop', 'BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'BatchAddSub', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'refadd', '-Edit', '-Delete', 'stop', 'grpadd', 'grpedit', 'grpdel', 'grpref', 'grpena', 'grpdis', 'cardrpt'];

        changeDisabled = ['Query', 'Refresh', 'moreop', 'BatchEdit', 'Print', 'Link2', 'MoreOp', 'Sort', 'AdjustSort', 'ExportPhoto', 'psndocCreateUser', 'File', 'assqry', 'ListReport', 'JobHis', 'ReptObj', 'Link7', 'ExportPsnInfo', 'ExportPsnInfoAll', 'Link6', 'Print', 'output', 'BatchAddSub', 'refadd', '-Edit', '-Delete', 'stop', 'grpadd', 'grpedit', 'grpdel', 'grpref', 'grpena', 'grpdis', 'cardrpt'];
    }
    // 新增/修改页面
    else if(page === 'addEditPage') {
        changeVisible = ['Save', 'Cancel'];
        changeDisabled = ['Save', 'Cancel'];
    }
    // 新增修改页面浏览态  
    else if(page === 'addEditPageBrowser') {
        changeVisible = ['Edit'];
        changeDisabled = ['Edit'];
    }
    // 调整顺序页面
    else if(page === 'adjustSort') {
        changeVisible = ['Save', 'Cancel'];
        changeDisabled = ['Save', 'Cancel'];
    }


    changeVisible.forEach((key) => {
        buttonVisible[key] = true;
    });
    changeDisabled.forEach((key) => {
        buttonDisable[key] = false;
    });


    button.setButtonVisible(buttonVisible);
    button.setButtonDisabled(buttonDisable);

    
    // 左侧树
    let {disabledTreeNodeList, currentSelectDepartment} = this.state;
    let currentDisabled = disabledTreeNodeList.includes(currentSelectDepartment);
    this.props.button.setButtonDisabled({
        grpadd: false,
        grpedit: !currentSelectDepartment,
        grpdel: !currentSelectDepartment,
        grpref: false,
        grpena: !currentSelectDepartment || !currentDisabled,
        grpdis: !currentSelectDepartment || currentDisabled,
        stop: !currentSelectDepartment
    });
}